package com.kingdee.openimport.patch_service.util;

import com.kingdee.openimport.patch_service.enume.ErrorCode;
import com.kingdee.openimport.patch_service.exception.PatchException;

/**
 * Created by Niki on 2018/5/8 10:35
 */
public class ResultUtil {

    private static final String SUCCESS_CODE = "100";
    public static <T> Result<T> success(){
        return new Result<T>(true, null, SUCCESS_CODE);
    }

    public static <T> Result<T> success(String msg){
        return new Result<T>(true,msg,SUCCESS_CODE);
    }

    public static <T> Result<T> success(T data){
        return new Result<T>(true,null,SUCCESS_CODE, data);
    }

    public static <T> Result<T> success(String msg, T data){
        return new Result<T>(true,msg,SUCCESS_CODE, data);
    }

    public static <T> Result<T> error(String msg, String code){
        //code不是数字
        try {
            Integer.valueOf(code);
        } catch (Exception e) {
            code = "未知错误码！";
            msg = "出现异常，请联系管理员！";
        }
        return new Result<T>(false, msg, code);
    }

    public static <T> Result<T> error(String msg, String code, T data){
        Result<T> result = error(msg, code);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(String code){
        return new Result<T>(false, "调用出错！", code);
    }

    public static <T> Result<T> error(String code, T data){
        return new Result<T>(false, "调用出错！", code, data);
    }

    public static <T> Result<T> error(Exception e){
        String errCode = e.getMessage();
        String msg = ErrorCode.getMsgByCode(errCode);
        return error(msg,errCode);
    }

    public static<T> Result<T> error(PatchException e){
        return error(e.getMsg(), e.getCode()) ;
    }

}
